/**
 * 
 */
package com.gmail.blackdog1987.onedollar.service.impl;

import com.gmail.blackdog1987.onedollar.dao.AdDao;
import com.gmail.blackdog1987.onedollar.dao.NoticeDao;
import com.gmail.blackdog1987.onedollar.dao.PrivacyDao;
import com.gmail.blackdog1987.onedollar.entity.AppAdEntity;
import com.gmail.blackdog1987.onedollar.entity.PrivacyEntity;
import com.gmail.blackdog1987.onedollar.entity.ResultJson;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.service.SystemService;
import com.gmail.blackdog1987.onedollar.util.JsonChangeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author 董国超
 *
 */
@Service("systemService")
public class SystemServiceImpl implements SystemService {
	@Autowired
	private PrivacyDao privacyDao;
	@Autowired
	private AdDao adDao;
	@Autowired
	private NoticeDao noticeDao;
	@Override
	public Map<String, Object> getAds(Map<String, String> filter) {
		ResultJson resultJson = JsonChangeUtil.setResultJson("0000", "请求成功");

		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> sort = new HashMap<String, Object>();
		param.put("startTime<=", new Date());
		param.put("endTime>=", new Date());
		param.put("status", new Integer(1));
		param.put("adPosition", new Integer(1));
		sort.put("startTime", "asc");
		List<AppAdEntity> list = adDao.find(param, sort);

		if(list==null||list.size()==0){
//			resultJson.put("code", ErrorCode.ORDER_PAY);
			return resultJson.getResultMap();
		}

		resultJson.put("data", list);

		return resultJson.getResultMap();
	}

	@Override
	public Map<String, Object> getNotice(Map<String, String> filter) {
		ResultJson resultJson = JsonChangeUtil.setResultJson("0000", "请求成功");
		String pageIndex = filter.get("pageindex");
		String pageSize = filter.get("pagesize");

		int index = 1;
		int size = 10;
		if(pageIndex!=null&&pageIndex.length()!=0){
			index = Integer.valueOf(pageIndex);
			if(index<1){
				index = 1;
			}
		}
		if(pageSize!=null&&pageSize.length()!=0){
			size = Integer.valueOf(pageSize);
			if(size==0){
				size = 10;
			}
		}

		Page page = noticeDao.queryForPage(index, size);

		resultJson.put("data", page);

		return resultJson.getResultMap();
	}

	@Override
	public Map<String, Object> doReadNotice(Map<String, String> filter) {
		ResultJson resultJson = JsonChangeUtil.setResultJson("0000", "请求成功");
		String id = filter.get("id");

		noticeDao.updateRead(Integer.valueOf(id));

		return resultJson.getResultMap();
	}

	@Override
	public Map<String, Object> getPrivacy(Map<String, String> filter) {
		ResultJson resultJson = JsonChangeUtil.setResultJson("0000", "请求成功");

		List<PrivacyEntity> list = privacyDao.list();

		if(list==null||list.size()==0){
//			resultJson.put("code", ErrorCode.ORDER_PAY);
			return resultJson.getResultMap();
		}
		return JsonChangeUtil.setResultJson(list.get(0)).getResultMap();
	}
}
